﻿(function ($) {
    $.fn.yomi = function () {
        var data = "";
        var _DOM = null;
        var TIMER;
        var range;
        var flag;
        createdom = function (dom) {
            _DOM = dom;
            data = $(dom).attr("data-finish");
            flag = $(dom).hasClass('start');
            data = data.replace(/-/g, "/");
            data = Math.round((new Date(data)).getTime() / 1000);
            range = data - Math.round((new Date()).getTime() / 1000);
            if(range <= 0){
                $(_DOM).html("活动已结束 查看活动详情&gt;&gt;");
            }else if(range > 0 && flag) {
                $(_DOM).html("距活动开始 <b class='d0'>0</b> <b class='d1'>1</b> 天 <b class='h0'>0</b> <b class='h1'>1</b> 小时 <b class='m0'>0</b> <b class='m1'>1</b> 分");
            }else if(range > 0 && !flag) {
                $(_DOM).html("距活动结束 <b class='d0'>0</b> <b class='d1'>1</b> 天 <b class='h0'>0</b> <b class='h1'>1</b> 小时 <b class='m0'>0</b> <b class='m1'>1</b> 分");
            }
            reflash();
        };
        reflash = function () {
            var range = data - Math.round((new Date()).getTime() / 1000),
                secday = 86400,
                sechour = 3600,
                day = parseInt(range / secday),
                hour = parseInt((range % secday) / sechour),
                minute = parseInt(((range % secday) % sechour) / 60),
                second = ((range % secday) % sechour) % 60;
            var dateAll = {
                d: day < 10 ? '0' + day : day + '',
                h: hour < 10 ? '0' + hour : hour + '',
                m: minute < 10 ? '0' + minute : minute + '',
                s: second < 10 ? '0' + second : second + ''
            };

            var d0 = dateAll.d.toString()[0],
                d1 = dateAll.d.toString()[1],
                h0 = dateAll.h.toString()[0],
                h1 = dateAll.h.toString()[1],
                m0 = dateAll.m.toString()[0],
                m1 = dateAll.m.toString()[1],
                s0 = dateAll.s.toString()[0],
                s1 = dateAll.s.toString()[1];

                $(_DOM).find(".d0").html(d0);
                $(_DOM).find(".d1").html(d1);
                $(_DOM).find(".h0").html(h0);
                $(_DOM).find(".h1").html(h1);
                $(_DOM).find(".m0").html(m0);
                $(_DOM).find(".m1").html(m1);
        };
        TIMER = setInterval(reflash, 1000);
        nol = function (h) {
            return h > 9 ? h : '0' + h;
        };
        return this.each(function () {
            var $box = $(this);
            createdom($box);
        });
    }
})(jQuery);

$(function () {
    $(".r-count").each(function () {
        $(this).yomi();
    });
});